﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class DkMono : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(waitBooter());
    }
    private void Awake()
    {
        OnAwake();
    }
    /// <summary>
    /// 用于组件初始化
    /// </summary>
    public abstract void OnAwake();
    /// <summary>
    /// 用于等待数据后进行模型初始化之类 
    /// </summary>
    public abstract void OnStart();

    IEnumerator waitBooter()
    {
        while (!Booter.Instance.inited) yield return 1;
        OnStart();
        yield break;
    }
}
public abstract class DkMonoSingleton<T> : DkMono where T : DkMonoSingleton<T>
{
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                var singList = FindObjectsOfType<T>();
                if (singList.Length > 0) _instance = singList[0];
                else if (singList.Length <= 0) Debug.LogError($"当前实例{typeof(T).Name}不存在");
                for (int i = 1; i < singList.Length; i++) Destroy(singList[i].gameObject);
            }
            return _instance;
        }
    }

    protected static T _instance;

    public override void OnAwake()
    {
        if (Instance != null && Instance != this) { Destroy(this.gameObject); return; }

        else { Debug.Log(this.gameObject.name); }
    }
}
